package com.gopay.bis.tbank.mtpay.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.tbank.mtpay.operation.MtPayBuildMessage;
import com.gopay.bis.tbank.mtpay.operation.MtPayParserMessage;
import com.gopay.common.constants.bank.MtPayBankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.MtPayBaseMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.businesses.MtPayService;


/**
 * 测试银行，无卡支付
 * 
 * @TBankMtServiceImpl.java
 * @author WangHongFei
 */
public class TBankMtServiceImpl implements MtPayService {
	
    private HttpTransport httpTransport;
    private MtPayBuildMessage mtPayBuildMessage;
    private MtPayParserMessage mtPayParserMessage;


	@Override
	public Object pay(BaseMessage obj) throws GopayException {
		return null;
	}

	@Override
	public Object verify(BaseMessage obj) throws GopayException {
		return null;
	}
    
    // 无卡支付暂用此类 仅仅是为了测试
    public Object query(BaseMessage object) throws GopayException{
        MtPayBaseMessage retMtPayMessage = new MtPayBaseMessage();
        try {
            Object tempObj = null;
            Map tempMap = null;
            MtPayBaseMessage mtPayBaseMessage = (MtPayBaseMessage) object;
            this.httpTransport.setUrl(mtPayBaseMessage.getBankUrl());                
            if (MtPayBankConstants.MTTPAY_PAY.equals(mtPayBaseMessage.getMtPayOperType())) {// 支付
                tempMap = this.mtPayBuildMessage.buildMtPay(mtPayBaseMessage);
                BankLogUtils.printMtPayQueryReqLog(mtPayBaseMessage, tempMap);
                tempObj = this.httpTransport.submit(tempMap);
                retMtPayMessage = this.mtPayParserMessage.parseMtPay(tempObj);
            } else if (MtPayBankConstants.MTTPAY_FLUSH.equals(mtPayBaseMessage.getMtPayOperType())) {// 冲正
                tempMap = this.mtPayBuildMessage.buildMtPayFlush(mtPayBaseMessage);
                BankLogUtils.printMtPayQueryReqLog(mtPayBaseMessage, tempMap);
                tempObj = this.httpTransport.submit(tempMap);
                retMtPayMessage = this.mtPayParserMessage.parseMtPayFlush(tempObj);
            }
            processMtPayMessage(retMtPayMessage);
            BankLogUtils.printMtPayParseMsgLog(mtPayBaseMessage, retMtPayMessage);
            return retMtPayMessage;
        }catch (Exception e) {
        	BankLogUtils.printMtPayExceptionLog(object, e);
        	throw new GopayException(ProcCodeConstants.PROC_CODE_100E6059);
        }
    }

    private void processMtPayMessage( MtPayBaseMessage retMtPayMessage){
        String tempStr = (String) (retMtPayMessage.getMtbPayHashMap().get("retCd"));
        if(StringUtils.isNotBlank(tempStr)&&(tempStr.trim().endsWith("999"))){
            retMtPayMessage.setVerifyFailure(true);
        }
    }

    public void setHttpTransport(HttpTransport httpTransport) {
        this.httpTransport = httpTransport;
    }

	public void setExPayBuildMessage(MtPayBuildMessage mtPayBuildMessage) {
		this.mtPayBuildMessage = mtPayBuildMessage;
	}

	public void setExPayParserMessage(MtPayParserMessage mtPayParserMessage) {
		this.mtPayParserMessage = mtPayParserMessage;
	}

}
